class BlackDeathSocketedInventoryManager extends BlackDeathInventoryManager
	config(Game);

/**Weapon types:
0 - None
1 - fists
2 - sword
3 - Torch
Expand as neccisary
*/

var array < int > HolderSocketNums;

var array < BlackDeathWeapon > SwordHolders;
var array < BlackDeathWeapon > TorchHolders;

event PostBeginPlay()
{
	Super.PostBeginPlay();
	AssignHolderNums(HolderSocketNums);
}

function AssignHolderNums(Array < int > HolderNums) //assigns length to holder based on default, holder length tell how many of a type of item the character can hold
{
	SwordHolders.Length = HolderNums[2];
	TorchHolders.Length = HolderNums[3];
}

function int FindEmptyHolder(array < BlackDeathWeapon > Holders) //retruns the first index in the array that has none, i.e. is empty, or -1
{
	return Holders.find(None);
}

function bool GetHolderArray(BlackDeathWeapon BDW, out Array < BlackDeathWeapon > Holder, optional out Name ArrayName) //assigns top out varible a wepaons holder and holder named based on weapon enumeration var
{
	switch(BDW.WeaponType)
	{
		case 0: case 1: return true;
		case 2:
			ArrayName = NameOf(SwordHolders); Holder = SwordHolders; return true;
		case 3:
			ArrayName = NameOf(TorchHolders); Holder = TorchHolders; return true;
	}
	return false;
}

function bool SetHolderArray(BlackDeathWeapon BDW, Array < BlackDeathWeapon > Holder) //reassigns holder back into global varible (needed becuse of reference passing)
{
	switch(BDW.WeaponType)
	{
		case 0: case 1: return true;
		case 2: SwordHolders = Holder; return true;
		case 3: TorchHolders = Holder; return true;
	}
	return false;
}

function PlaceInHolder(BlackDeathWeapon BDW) //Gets the weapons holder and if the holder has a length, and thus exists, then finds an empty spot. If a spot exists then the weapon si assigned into the holder array and attached int he appropraite socket spot
{
	local int holderIndex;
	local Name WeaponSocket, ArrayName;
	local Array < BlackDeathWeapon > Holders;
	Holders.length = 0;
	GetHolderArray(BDW, Holders, ArrayName);
	if(Holders.length != 0)
	{
		`log("Putting in holder: "@BDW);
		holderIndex = FindEmptyHolder(Holders);
		if(holderIndex != -1)
		{
			Holders[holderIndex] = BDW;
			WeaponSocket = Name(ArrayName$holderIndex);
			if(Instigator.Mesh.GetSocketByName(WeaponSocket) != none)
			{
				BDW.AttachWeaponTo(Instigator.Mesh, WeaponSocket);
			}
			SetHolderArray(BDW, Holders);
		}
	}
}

simulated function RemoveFromHolder(BlackDeathWeapon BDW) //Gets the wepon holder array and find the wepon in the array. It sets the array spot to None if found or logs an error if the wepon isn't found.
{
	local Array < BlackDeathWeapon > Holders;
	local int indexNum;
	GetHolderArray(BDW, Holders);
	if(Holders.length != 0)
	{
		indexNum = Holders.Find(BDW);
		if(indexNum != -1)
		{
			Holders[indexNum] = None;
			SetHolderArray(BDW, Holders);
		}
		else
		{
			`log("ERROR: could not find wepaon "@BDW@" in holders");
		}
	}
}

simulated function bool AddInventory(Inventory NewItem, optional bool bDoNotActivate)
{
	local Inventory Item, LastItem;
	local BlackDeathWeapon BDW;
	// The item should not have been destroyed if we get here.
	if( (NewItem != None) && !NewItem.bDeleteMe )
	{
		// if we don't have an inventory list, start here
		if( InventoryChain == None )
		{
			InventoryChain = newItem;
		}
		else
		{
			// Skip if already in the inventory
			for (Item = InventoryChain; Item != None; Item = Item.Inventory)
			{
				if( Item == NewItem )
				{
					return FALSE;
				}
				LastItem = Item;
			}
			LastItem.Inventory = NewItem;
		}

		`LogInv("adding" @ NewItem @ "bDoNotActivate:" @ bDoNotActivate);
		
		NewItem.SetOwner( Instigator );
		NewItem.Instigator = Instigator;
		NewItem.InvManager = Self;
		//NewItem.GivenTo( Instigator);
		
		BDW = BlackDeathWeapon(NewItem); //Sockets the weapon immediatly
		if(BDW != none)
		{
			PlaceInHolder(BDW);
		}

		// Trigger inventory event
		Instigator.TriggerEventClass(class'SeqEvent_GetInventory', NewItem);
		return TRUE;
	}

	return FALSE;
}

simulated function RemoveFromInventory(Inventory ItemToRemove)
{
	local Inventory Item;
	local bool		bFound;

	if( ItemToRemove != None )
	{
		if( InventoryChain == ItemToRemove )
		{
			bFound = TRUE;
			InventoryChain = ItemToRemove.Inventory;
		}
		else
		{
			// If this item is in our inventory chain, unlink it.
			for(Item = InventoryChain; Item != None; Item = Item.Inventory)
			{
				if( Item.Inventory == ItemToRemove )
				{
					bFound = TRUE;
					Item.Inventory = ItemToRemove.Inventory;
					break;
				}
			}
		}

		if( bFound )
		{
			`LogInv("removed" @ ItemToRemove);
			ItemToRemove.ItemRemovedFromInvManager();
			ItemToRemove.SetOwner(None);
			ItemToRemove.Inventory = None;
			if(BlackDeathWeapon(ItemToRemove) != none)
			{
				RemoveFromHolder(BlackDeathWeapon(ItemToRemove));//remove wepon from socket to clean up
			}
		}

		// make sure we don't have other references to the item
		if( ItemToRemove == Instigator.Weapon )
		{
			Instigator.Weapon = None;
		}

		if( Instigator.Health > 0 && Instigator.Weapon == None && Instigator.Controller != None )
		{
			`LogInv("Calling ClientSwitchToBestWeapon");
			Instigator.Controller.ClientSwitchToBestWeapon(TRUE);
		}
	}
}


/** Stuff to cut out later if test work, remove if found 
function AnimAttachWeaponToHand()
{
	if(Instigator.Weapon != none)
	{
		`log("Attaching to hand"@Instigator.Weapon);
		RemoveFromHolder(BlackDeathWeapon(Instigator.Weapon));
		BlackDeathWeapon(Instigator.Weapon).AttachWeaponTo( Instigator.Mesh, BlackDeathPawn(Instigator).WeaponSocket);
	}
}

function AnimRemoveWeaponFromHand()
{
	PlaceInHolder(BlackDeathWeapon(Instigator.Weapon));
}
*/

defaultproperties
{
	HolderSocketNums = (2,2,2,2)
}